package com.wk.worksetter.worker

import android.app.Activity
import android.content.Context
import android.content.pm.PackageInfo
import androidx.work.Data
import androidx.work.Worker
import androidx.work.WorkerParameters
import com.wk.worksetter.BaseApplication
import com.wk.worksetter.utils.WorkUtil

/**
 * @author peng
 * @date 2021/1/28
 * 说明
 */
class StartAppWorker(val context: Context, val workerParams: WorkerParameters) :
    Worker(context, workerParams) {

    override fun doWork(): Result {
        try {
            Thread.sleep(10_000)
            val packageName = workerParams.inputData.getString("packageName")
            if (context is Activity) {
                packageName?.let { WorkUtil.launchByPackage(context, it) }
            }else{
                packageName?.let { WorkUtil.launchByPackage(BaseApplication.ins, it) }
                return Result.success(Data.Builder().putString("error","context不是Activity,用Application了").build())

            }
        } catch (e: Exception) {
            return Result.failure(Data.Builder().putString("error",e.message).build())
        }
        return Result.success()
    }

}